package com.jsjosem.aftersales.interceptor;

import androidx.annotation.NonNull;

import com.jsjosem.aftersales.cache.UserCache;
import com.jsjosem.aftersales.constant.CommonConstant;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class AccessTokenInterceptor implements Interceptor {

    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        if (UserCache.getInstance().getToken() == null) {
            return chain.proceed(chain.request());
        }

        Request original = chain.request();
        Request.Builder requestBuilder = original.newBuilder()
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("cache-control", "no-cache,private")
                .addHeader("language", "zh_cn")
                .addHeader("accept", "application/json")
                .addHeader(CommonConstant.TOKEN_HEADER, CommonConstant.TOKEN_PREFIX + UserCache.getInstance().getToken());
        Request request = requestBuilder.build();
        return chain.proceed(request);
    }
}